1. /* sifissub.cpp by K.Tsuru */
  2. // function ID = 423 BRADIX
  3. /************************************************
  4. SInteger class
  5. It provides a subtraction "SInteger-unsigned short".
  6. It is used in operator--();
  7. result = m - s ( s < BRADIX)
  8. *************************************************/
  9. #ifndef SN_H
  10. #include "sn.h"
  11. #endif
  12. void IsSub(const SInteger& m, fType s, SInteger& result){
  13. if(s >= BRADIX) m.SetError(m.OUT_OF_RANGE, "IsSub", 423);
  14. result = m; // copy
  15. if(!s) return;
  16. // result == m
  17. if(m.Sign(423)<0){ // m < 0
  18. // m - s = -(|m|+s)
  19. result.SetSign(1); IsAdd(result, s, result); result.SetSign(-1);
  20. return;
  21. }
  22. // ------ In the case of m > 0 ------
  23. fType u = result.figure(0) - s; //u>BRADIX for result.figure(0)<s
  24. if( (m.aHead == 0) && (u == 0) ){ // m == s
  25. result.SetZero(); return;
  26. }
  27. register uint i = 0;
  28. if(u < BRADIX){
  29. result.figure[0] = u;
  30. while( !result.figure(i) ) i++;
  31. result.aTail = i;
  32. return;
  33. }
  34. // u >= BRADIX ( m(0) < s )
  35. if(!m.aHead){ // m has one figure and m < s.
  36. result.figure[0] = s - m[0];
  37. result.SetSign(-1);
  38. return;
  39. }
  40. // Normalize u >= BRADIX and m.aHead >= 1
  41. fType* rv = result.figure.Elements();
  42. rv[0] = u & BRADIX1;
  43. u = u >> BRADIX_BITS; //borrow u =1
  44. i = 0;
  45. while(u){
  46. i++;
  47. u = rv[i] - u;
  48. rv[i] = u & BRADIX1;
  49. u = u >> BRADIX_BITS;
  50. }
  51. i = m.aHead;
  52. #ifndef NDEBUG
  53. result.figure(i);
  54. #endif
  55. while(!rv[i]) i--;
  56. result.aHead = i;
  57. i = 0;
  58. while(!rv[i]) i++;
  59. result.aTail = i;
  60. if(2u*(result.aHead+1) <= result.figure.size()) result.DoCutDown();
  61. }

sifissub.cpp : last modifiled at 2017/03/13 14:31:59(1,590 bytes)
created at 2016/04/25 14:53:17
The creation time of this html file is 2017/10/25 11:09:45 (Wed Oct 25 11:09:45 2017).